package com.sw.haruka.view;


import android.widget.Toast;

import com.sw.haruka.R;
import com.sw.haruka.model.BaseActivity;
import com.sw.haruka.model.entity.FileExplorer;
import com.sw.haruka.helper.utils.FileUtils;
import com.sw.haruka.dal.HarukaConfig;
import com.sw.haruka.helper.utils.MediaStoreUtils;
import com.sw.haruka.module.title.TitleMode;
import com.sw.haruka.module.title.TransformerTitleFragment;

import java.util.ArrayList;
import java.util.HashSet;

public class FolderActivity extends BaseActivity {

    private FileExplorer mExplorer;

    private String mPath;   // 传来的路径
    private String FOLDER_MODE = "move";    // 模式，默认是“移动文件/文件夹”模式

    /*
    移动文件的模式
     */
    private ArrayList<String> mMoveFilePathArray;

    @Override
    protected int layoutId() {
        return R.layout.activity_folder;
    }

    @Override
    protected void initView() {
        FOLDER_MODE = getIntent().getStringExtra("mode");
        int titleMode = TitleMode.FOLDER_BAR;
        if (FOLDER_MODE != null && FOLDER_MODE.equals("move")) {
            // move模式
            mPath = HarukaConfig.ROOT_PATH;
            mMoveFilePathArray = getIntent().getStringArrayListExtra("moveFilePathArray");
            titleMode = TitleMode.MOVE_BAR;
        } else {
            mPath = getIntent().getStringExtra("path");
        }
        TransformerTitleFragment titleFragment = new TransformerTitleFragment(titleMode);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.title_ll, titleFragment)
                .commit();
        if (titleMode == TitleMode.MOVE_BAR) {
            titleFragment.setFolderDo(new TransformerTitleFragment.IFolderDo() {
                @Override
                public void moveToThis() {
                    HashSet<String> paths = new HashSet<>();
                    for (String path : mMoveFilePathArray) {
                        if (!FileUtils.moveFile(path, mExplorer.getCurrentPath(), paths)) {
                            Toast.makeText(FolderActivity.this, R.string.move_failure, Toast.LENGTH_SHORT).show();
                        }
                    }
                    MediaStoreUtils.INSTANCE.refresh(paths.toArray(new String[0]));
                    finish();
                }
            });
        }
        mExplorer = findViewById(R.id.folder_explorer);
        mExplorer.setNoStorageBar(true);
        if (FOLDER_MODE != null && FOLDER_MODE.equals("move")) {
            mExplorer.setNoSelectTool(true);
            mExplorer.initList(HarukaConfig.ROOT_PATH);
        } else {
            mExplorer.initList(mPath);
        }
    }

    @Override
    protected void loadData(boolean isRefresh) {

    }

    @Override
    public void onBackPressed() {
        if (mExplorer.back()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mExplorer != null) {
            mExplorer.refresh();
        }
    }
}
